package controller.shop;

import model.ShopCar;
import model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import service.IGoodsService;
import service.IOrderService;
import service.IShopCarService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @aythor:侯仪澳
 * @data:2022/8/4
 */
@Controller
@RequestMapping("car")
public class ShopCarController {
    @Autowired
    private IShopCarService shopCarService;
    @Autowired
    private IGoodsService goodsService;

    //跳转到购物车页面
    @RequestMapping("carPage")
    public ModelAndView carPage(ShopCar shopCar, HttpServletRequest req){
        //创建session
        HttpSession session = req.getSession();
        //从session中获取管理员对象
        User user = (User) session.getAttribute("u");
        ShopCar shopCar1 = new ShopCar();
        shopCar1.setgName(shopCar.getgName());
        shopCar1.setuId(user.getId());
        List<ShopCar> list = shopCarService.queryShopCar(shopCar1);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("gName", shopCar.getgName());
        modelAndView.addObject("list", list);
        modelAndView.setViewName("shopping/mall_shopCart");
        return modelAndView;
    }

    //添加到购物车
    @RequestMapping("addOneGoodsToCar")
    @ResponseBody
    private String addOneGoodsToCar(ShopCar shopCar){
        return shopCarService.addOneGoodsToCar(shopCar);
    }

    //删除购物车里的商品
    @RequestMapping("deleteOneGoodsInCar")
    @ResponseBody
    private String deleteOneGoodsInCar(Integer id){
        return shopCarService.deleteShopCar(id);
    }

}
